/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useRef, useState } from 'react';
import type { formType } from '../../../../../form/index';
import ProForm, { ModalForm } from '@ant-design/pro-form';
import type { ProFormInstance } from '@ant-design/pro-form';
import { ProFormText, ProFormTextArea, ProFormDigit } from '@ant-design/pro-form';
import { refundReview, getBalance } from '@/services/tools/refundRequest';
import { message, Typography } from 'antd';
import { isFunction } from 'lodash';
import type { Columns } from '@/components/_pages/tools/refundRequest/_utils/type';

interface KbRefundModalProps extends formType {
  record: Columns;
  modalType: 'pass' | 'refuse';
  callback: () => void;
}

const { Text } = Typography;
const KbRefundModal = (props: KbRefundModalProps | any) => {
  const refundRef = useRef<any>({ loading: false });
  const formRef = useRef<ProFormInstance>();
  const [realMoney, setRealMoney] = useState<string>('0');
  const { record, modalType, callback, visible, type, ...rest } = props;
  const isPass = modalType === 'pass';
  const status = isPass ? '1' : '2';
  const title = isPass ? '退款' : '拒绝退款';

  const onFinish = async (values: any) => {
    if (isPass && Number(values.refund_money * 1) > Number(realMoney)) {
      message.error('退款金额不能超过余额');
      return;
    }
    const reqData = { ...values, status };
    if (isPass) {
      reqData.money = values.refund_money;
    }
    if (refundRef.current.loading) {
      return;
    }
    refundRef.current.loading = true;
    refundReview(reqData).then((res) => {
      refundRef.current.loading = false;
      const { code, msg } = res;
      if (code == 0) {
        if (callback && isFunction(callback)) {
          message.success('操作成功');
          callback();
        }
      } else {
        message.error(msg);
      }
    });
  };

  const getMoney = (id: string) => {
    getBalance({ id }).then((res) => {
      const { code, data, msg } = res;
      if (code == 0) {
        setRealMoney(data.money);
      } else {
        message.error(msg);
      }
    });
  };

  useEffect(() => {
    if (visible && record.id) {
      formRef.current?.setFieldsValue({
        ...record,
        refund_money: null,
      });
      getMoney(record.id);
    }
  }, [record, visible]);

  const formFields = (
    <>
      <ProFormText hidden name='id' />
      <ProFormText readonly label='用户账号' name='user_id' />
      <ProFormText readonly label='产品类型' name='kb_name' />
      <ProFormText
        readonly
        label='申请金额'
        name='money'
        addonAfter={<Text type='secondary'>账户实时余额{realMoney} 元</Text>}
      />
      <ProFormDigit
        label='退款金额'
        name='refund_money'
        hidden={!isPass}
        addonAfter='元'
        fieldProps={{ precision: 2 }}
        rules={isPass ? [{ required: true, message: '请输入退款金额' }] : []}
      />
      <ProFormTextArea
        label='拒绝原因'
        hidden={isPass}
        name='reason'
        placeholder='请填写本次拒绝的原因，非必填'
      />
    </>
  );
  return (
    <>
      {type === 'form' ? (
        <ProForm title={title} formRef={formRef} {...rest} visible={visible} onFinish={onFinish}>
          {formFields}
        </ProForm>
      ) : (
        <ModalForm title={title} formRef={formRef} {...rest} visible={visible} onFinish={onFinish}>
          {formFields}
        </ModalForm>
      )}
    </>
  );
};

export default KbRefundModal;
